package com.example.administrator.myapplication.muscle;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.NumberPicker;
import android.widget.Toast;


import com.example.administrator.myapplication.BmobApplication;
import com.example.administrator.myapplication.R;
import com.example.administrator.myapplication.evaluate.TestAssess;


public class muscle_main_Fragment extends Fragment   {
    private Button muscle_submit_bt;
    private ImageView muscle_boy_pic;
    private ImageView muscle_girl_pic;
    private Boolean flag_boy=false;
    private Boolean flag_girl=false;
    private Integer value ;
    private BmobApplication muscle_apps;
    private Button bt_muscle_choose;
    private Toolbar toolbar;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_muscle_main_, container, false);
        toolbar= (Toolbar) getActivity().findViewById(R.id.toolbar_muscle);
        toolbar.setNavigationIcon(R.mipmap.custom_back);
        toolbar.setTitle("肌力&肌耐力适能");
        toolbar.setTitleMarginStart(100);
        toolbar.setTitleTextColor(Color.parseColor("#ffffff"));
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().finish();
            }
        });

        muscle_boy_pic =(ImageView)  view.findViewById(R.id.muscle_selected_boy);
        muscle_girl_pic =(ImageView) view.findViewById(R.id.muscle_selected_girl);


        init(view);

        bt_muscle_choose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                createNumberpicker();
            }
        });



        //判断是否为男，如果为男，则自动选择图片 默认为男
        if(muscle_apps.is_man()){
            flag_boy=true;
            muscle_boy_pic.setImageResource(R.drawable.muscle_option_boy_selected);

            muscle_girl_pic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(getActivity(),"当前用户为男性",Toast.LENGTH_SHORT).show();
                }
            });

        }else{
            flag_girl=true;
            muscle_girl_pic.setImageResource(R.drawable.muscle_option_girl_selected);

            muscle_boy_pic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(getActivity(),"当前用户为女性",Toast.LENGTH_SHORT).show();
                }
            });

        }




        muscle_submit_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str =value+"";
                int musclescore;
                if (muscle_apps.is_man()){
                    if (value <= 2){
                        musclescore = 0;
                    }
                    else if (value  > 2 && value < 13){
                        musclescore = (int)(9.0909 * (value - 2));
                    }
                    else {
                        musclescore = 100;
                    }
                }
                else {
                    if (value <= 16){
                        musclescore = 0;
                    }
                    else if (value  > 16 && value < 54){
                        musclescore = (int)(2.6316* (value - 16));
                    }
                    else {
                        musclescore = 100;
                    }
                }
                muscle_apps.setMuscle_result(String.valueOf(musclescore));
                muscle_apps.setFlag(2);
                muscle_apps.setIntentflag(0);
                Intent intent =new Intent(getActivity(), TestAssess.class);
                getActivity().finish();
                startActivity(intent);


            }
        });

        return view;
    }

    public void createNumberpicker(){
        View view =LayoutInflater.from(getActivity()).inflate(R.layout.activity_numberpicker,null);
        final NumberPicker muscle_numberpicker = (NumberPicker) view.findViewById(R.id.test_numberpicker);
        muscle_numberpicker.setMinValue(0);
        muscle_numberpicker.setMaxValue(200);
        muscle_numberpicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                value=newVal;
            }
        });

        Dialog dialog  =new AlertDialog.Builder(getActivity()).setView(view).setTitle("请选择次数").setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                bt_muscle_choose.setTextColor(Color.parseColor("#ff0000"));
                bt_muscle_choose.setText("已选择");
                dialog.dismiss();

            }
        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        }).create();
        dialog.show();
    }

    public void init(View v){
        bt_muscle_choose= (Button) v.findViewById(R.id.bt_muscle_choose);
        muscle_boy_pic=(ImageView)v.findViewById(R.id.muscle_selected_boy);
        muscle_girl_pic=(ImageView)v.findViewById(R.id.muscle_selected_girl);
        muscle_submit_bt=(Button)v.findViewById(R.id.muscle_submit_bt);
        muscle_apps= (BmobApplication) getActivity().getApplication();
    }
}
